Skip to content

Conversation

@bwoebi
Copy link
Member

@bwoebi bwoebi commented Jan 8, 2026

b0af9ac removed the live-range splitting of foreach variables, however it only added handling to ZEND_HANDLE_EXCEPTION.
This was sort-of elegant, until it was realized in 8258b77 that it would leak the return variable, requiring some more special handling.
At some point we added live tmpvar rooting in 52cf7ab, but this did not take into account already freed loop variables, which also might happen during ZEND_RETURN, which cannot be trivially accounted for, without even more complicated handling in zend_gc_*_tmpvars() functions.

This commit also proposes a simpler way of tracking the loop end in loopvar freeing ops: handle it directly during live range computation rather than during compilation, eliminating the need for opcache to handle it specifically.
Further, opcache was using live_ranges in its basic block computation in the past, which it no longer does. Thus this complication is no longer necessary and this approach should be actually simpler now.

Closes #20766.

Copy link
Member

@arnaud-lb arnaud-lb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks right to me!

@bwoebi bwoebi force-pushed the bob/split-live-range branch from fed200c to 2d0f88e Compare January 9, 2026 13:08
@bwoebi bwoebi changed the base branch from PHP-8.3 to PHP-8.4 January 9, 2026 13:58
cataphract and others added 3 commits January 15, 2026 14:50
When FE_FREE with ZEND_FREE_ON_RETURN frees the loop variable during
an early return from a foreach loop, the live range for the loop
variable was incorrectly extending past the FE_FREE to the normal
loop end. This caused GC to access the already-freed loop variable
when it ran after the RETURN opcode, resulting in use-after-free.

Fix by splitting the ZEND_LIVE_LOOP range when an FE_FREE with
ZEND_FREE_ON_RETURN is encountered:
- One range covers the early return path up to the FE_FREE
- A separate range covers the normal loop end FE_FREE
- Multiple early returns create multiple separate ranges

# Conflicts:
#	Zend/tests/gc_048.phpt
#	Zend/tests/gc_049.phpt
b0af9ac removed the live-range splitting of foreach variables, however it only added handling to ZEND_HANDLE_EXCEPTION.
This was sort-of elegant, until it was realized in 8258b77 that it would leak the return variable, requiring some more special handling.
At some point we added live tmpvar rooting in 52cf7ab, but this did not take into account already freed loop variables, which also might happen during ZEND_RETURN, which cannot be trivially accounted for, without even more complicated handling in zend_gc_*_tmpvars() functions.

This commit also proposes a simpler way of tracking the loop end in loopvar freeing ops: handle it directly during live range computation rather than during compilation, eliminating the need for opcache to handle it specifically.
Further, opcache was using live_ranges in its basic block computation in the past, which it no longer does. Thus this complication is no longer necessary and this approach should be actually simpler now.

Closes php#20766.

Signed-off-by: Bob Weinand <[email protected]>
Signed-off-by: Bob Weinand <[email protected]>
@bwoebi bwoebi force-pushed the bob/split-live-range branch from 2d0f88e to 3a0e097 Compare January 15, 2026 13:51
Signed-off-by: Bob Weinand <[email protected]>
@bwoebi bwoebi merged commit 27ed48c into php:PHP-8.4 Jan 15, 2026
9 of 10 checks passed
bwoebi added a commit that referenced this pull request Jan 15, 2026
* 'PHP-8.4' of github.com:php/php-src:
  Split the live-ranges of loop variables again (#20865)
bwoebi added a commit that referenced this pull request Jan 15, 2026
* PHP-8.5:
  Split the live-ranges of loop variables again (#20865)
@github-actions

This comment was marked as outdated.

@github-actions
Copy link

AWS x86_64 (c7i.24xl)

Attribute Value
Environment aws
Instance type c7i.metal-24xl (dedicated)
Architecture x86_64
CPU Intel(R) Xeon(R) Platinum 8488C, 48 cores @ 2400 MHz
CPU settings disabled deeper C-states, disabled turbo boost, disabled hyper-threading
RAM 188 GB
Kernel 6.1.158-178.288.amzn2023.x86_64
OS Amazon Linux 2023.9.20251117
GCC 14.2.1
Time 2026-01-17 14:27:24 UTC
Job details (Artifacts)

Laravel 12.11.0 demo app - 100 consecutive runs, 50 warmups, 100 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skew P-value Memory
PHP - baseline@2c11 0.46201 0.46455 0.00053 0.12% 0.46275 0.00% 0.46259 0.00% 1.826 0.999 27.59 MB
PHP - bob/split-live-range 0.46214 0.46925 0.00094 0.20% 0.46303 0.06% 0.46279 0.04% 3.895 0.000 27.59 MB

Symfony 2.8.0 demo app - 100 consecutive runs, 50 warmups, 100 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skew P-value Memory
PHP - baseline@2c11 0.76028 0.77696 0.00218 0.28% 0.76787 0.00% 0.76795 0.00% -0.469 0.999 27.62 MB
PHP - bob/split-live-range 0.76317 0.77380 0.00256 0.33% 0.77051 0.34% 0.77134 0.44% -1.578 0.000 27.62 MB

Wordpress 6.9 main page - 100 consecutive runs, 20 warmups, 20 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skew P-value Memory
PHP - baseline@2c11 0.66454 0.68467 0.00209 0.31% 0.66600 0.00% 0.66575 0.00% 7.510 0.999 27.62 MB
PHP - bob/split-live-range 0.66195 0.68301 0.00440 0.66% 0.66405 -0.29% 0.66299 -0.41% 3.983 0.000 27.62 MB

bench.php - 100 consecutive runs, 10 warmups, 2 requests (sec)

PHP Min Max Std dev Rel std dev % Mean Mean diff % Median Median diff % Skew P-value Memory
PHP - baseline@2c11 0.41914 0.42688 0.00170 0.40% 0.42299 0.00% 0.42292 0.00% 0.099 0.999 7.95 MB
PHP - bob/split-live-range 0.42014 0.42657 0.00136 0.32% 0.42276 -0.05% 0.42273 -0.04% 0.317 0.331 7.95 MB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants